#include "sec_common.h"

unsigned char *endian_change(unsigned char *in,int len)		//len need 4byte align
{
	unsigned char tmp;
	int i;
	for(i=0;i<len;i+=4)
	{
		tmp = *in;
		*in = *(in+3);
		*(in+3) = tmp;
		tmp = *(in+1);
		*(in+1)=*(in+2);
		*(in+2)=tmp;
		in+=4;
	}

}

unsigned char *bin2s(unsigned char *b,unsigned char *s,int len)
{
  int i;
  unsigned char tmp;
  unsigned char *ret;
  ret = s;
  if(ret == NULL)
  {
    debug("bin2s fuction error,no output string\n");
    return NULL;
  }
  for(i=0;i<len;i++)
  {
    tmp=(*b&0xf0)>>4;
    if((tmp>=0) && (tmp<=9))
      tmp+='0';
    else if((tmp>=0xA)&&(tmp<=0xF))
      tmp=tmp-0xA+'A';
    else
      tmp=0;

    *s = tmp;s++;
    tmp=*b&0xf;
    if((tmp>=0) && (tmp<=9))
      tmp+='0';
    else if((tmp>=0xA)&&(tmp<=0xF))
      tmp=tmp-0xA+'A';
    else
      tmp=0;
    *s = tmp;s++;

    b++;
  }
  *s = 0;
  return ret;
}

union
{
  unsigned char b[8];
  unsigned short s[4];
  unsigned int i[2];
  unsigned long long l[1];
}st;

endin_test()
{
  unsigned int i,size;
  debug("mem for byte:\n");
  for(i=0;i<8;i++)
  {
    st.b[i]=i;
    debug("%x ",st.b[i]);
  }
  debug("\nmem for short:\n");
  for(i=0;i<4;i++)
  {
    debug("%x ",st.s[i]);
  }
  debug("\nmem for int:\n");
  for(i=0;i<2;i++)
  {
    debug("%x ",st.i[i]);
  }
  debug("\nmem for long long:\n");
  for(i=0;i<1;i++)
  {
    debug("%llx ",st.l[i]);
  }
  size = sizeof(char);
  debug("\nchar size is %x\n",size);
  size = sizeof(short);
  debug("short size is %x\n",size);
  size = sizeof(int);
  debug("int size is %x\n",size);
  size = sizeof(long);
  debug("long size is %x\n",size);
  size = sizeof(long long);
  debug("long long size is %x\n",size);

}

void set_rand_mem(unsigned char *ptr,unsigned size)
{
	unsigned tsize,i;
  //确保内存是4的倍数
	tsize = size&0xfffffffc;
	for(i=0;i<tsize;i+=4)
	{
		*(unsigned int *)ptr = rand();
		ptr+=4;
	}
	tsize = size&3;
	for(i=0;i<tsize;i++)
	{
		ptr[i] = rand()&0xff;
	}
}

void fail_prosess(void)
{
	//exit(0);
}
